//
//  ApplyActivityController.m
//  meiyinxiang
//
//  Created by 媒印象 on 05/12/2017.
//  Copyright © 2017 hbjApple. All rights reserved.
//

#import "ApplyActivityController.h"
#import "ApplySuccessView.h"

#import <NIMKitMediaFetcher.h>
#import <NIMSessionInteractorImpl.h>
#import "KGModal.h"
#import "VoteHelper.h"
@interface ApplyActivityController (){
    NIMSessionInteractorImpl *inder;
    NSString *_imgurl;
}
@property (weak, nonatomic) IBOutlet UIButton *addPhotoBtn;
@property (weak, nonatomic) IBOutlet UITextField *nameTextfield;
@property (weak, nonatomic) IBOutlet UITextField *ageTextfield;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextfield;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayoutCons;





@property (nonatomic,strong) NIMKitMediaFetcher *mediaFetcher;

@end

@implementation ApplyActivityController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)awakeFromNib{
    [super awakeFromNib];
     inder = [[NIMSessionInteractorImpl alloc] init];
    self.topLayoutCons.constant = [[VoteHelper shared] getTopLayoutConsWithdiffrentDivice];
}
//提交报名申请
- (IBAction)applyBtn:(id)sender {
    NSString *verifyStr = [[VoteHelper shared] verifyVoteEnableWithPhoneNum:self.phoneNumTextfield.text name:self.nameTextfield.text age:self.ageTextfield.text imgurl:_imgurl];
    if (verifyStr.length > 0 ) {
        ShowSVProgressErrorMessage(verifyStr);

//        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
//        [SVProgressHUD showInfoWithStatus:verifyStr];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
            
        });
        return;
    }
    [[VoteHelper shared] applyActivityWithPhoneNum:self.phoneNumTextfield.text
                                              name:self.nameTextfield.text
                                               age:self.ageTextfield.text
                                            imgurl:_imgurl
                                        completion:^( BOOL isSucess, NSString * msg) {
        if (isSucess) {
            ApplySuccessView *successView = [ApplySuccessView loadNibWithApplyView];
            [[KGModal sharedInstance] showWithContentView:successView];
        }
    }];
    
}
- (IBAction)backBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)addPhotoBtn:(id)sender {
    WS(weakSelf);
    [inder.mediaFetcher fetchPhotoFromLibrary:^(NSArray *images, NSString *path, PHAssetMediaType type) {
        if (images.count > 0) {
            UIImage *photoImg = images[0];
            [[VoteHelper shared] upLoadImg:photoImg completion:^(BOOL isSucess, NSString * imgurl) {
                  [weakSelf.addPhotoBtn setImage:photoImg forState:UIControlStateNormal];
                _imgurl = imgurl;
            }];
          
        }
    }];
}
//添加图片按钮


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NIMKitMediaFetcher *)mediaFetcher
{
    if (!_mediaFetcher) {
        _mediaFetcher = [[NIMKitMediaFetcher alloc] init];
    }
    return _mediaFetcher;
}
+ (ApplyActivityController *)loadNibApplyVC{
    UINib *nib = [UINib nibWithNibName:@"ApplyActivityController" bundle:[NSBundle mainBundle]];
    return [[nib instantiateWithOwner:nil options:nil] lastObject];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
